package bb.lanxing.lib.devices.sprint.model.impl;

import android.util.Pair;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sprint.Command;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.sprint.SprintListener;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.entity.SprintNav;
import bb.lanxing.lib.devices.sprint.utils.DeviceControllerHelper;
import bb.lanxing.lib.devices.sprint.utils.SprintFileHelper;
import java.io.File;
import java.util.Collections;
import java.util.List;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

public abstract class AbstractSprintDeviceNavigationModel extends AbstractSprintNavigationModel implements SprintListener {
    private String address;
    private XossDeviceController controller;

    @Override
    public void setAddress(String str) {
        String str2 = this.address;
        if (str2 == null || !str2.equals(str)) {
            this.address = str;
            XossDeviceController xossDeviceController = this.controller;
            if (xossDeviceController != null) {
                xossDeviceController.unregisterXossDeviceListener(this);
            }
            XossDeviceController xossDeviceController2 = DeviceControllerHelper.getXossDeviceController(str);
            this.controller = xossDeviceController2;
            if (xossDeviceController2 == null) {
                return;
            }
            xossDeviceController2.registerXossDeviceListener(this);
        }
    }

    @Override
    public void loadNavigation() {
        loadNavigationLushuFromSprint();
    }

    protected void loadNavigationLushuFromSprint() {
        File file = new File(DeviceHelper.getWorkDir(9), SprintFileHelper.NAV_FILE_NAME);
        if (file.exists()) {
            onFileReceived(file.getAbsolutePath());
        } else {
            this.controller.getFile(SprintFileHelper.NAV_FILE_NAME);
        }
    }

    @Override
    public void deleteByServerId(long j) {
        this.controller.delete(j + SprintFileHelper.NAV_FILE_STUFFIX);
    }

    @Override
    public void upload(String str) {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.sendFile(str);
        }
    }

    @Override
    public void abort() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.abort();
        }
    }

    @Override
    public void onProgressUpdate(Command command, float f) {
        if (this.progressUpdateSubject != null) {
            this.progressUpdateSubject.onNext(new Pair<>(Commands.parseFileName(command), f));
        }
    }

    @Override
    public void onCmdStatus(Command command, int i, byte[] bArr) {
        SprintNav navByName;
        String parseFileName = Commands.parseFileName(command);
        if (SprintFileHelper.isNavListFile(parseFileName)) {
            if (command.getCmd() != 5 || i == 0) {
                return;
            }
            this.lushuSubject.onNext(Collections.emptyList());
        } else if (!SprintFileHelper.isNavFile(parseFileName) || (navByName = getNavByName(parseFileName)) == null) {
        } else {
            byte cmd = command.getCmd();
            if (cmd == 7) {
                if (i == 0) {
                    SprintFileHelper.clearNavList(DeviceHelper.getWorkDir(9));
                    this.navigationStatusSubject.onNext(new Pair<>(navByName.getNavServerId(), DeviceFileStatus.STATUS_SYNCED.getStatus()));
                    return;
                }
                this.navigationStatusSubject.onNext(new Pair<>(navByName.getNavServerId(), DeviceFileStatus.STATUS_SYNC_FAIL.getStatus()));
            } else if (cmd != 13) {
            } else {
                if (i == 0) {
                    SprintFileHelper.clearNavList(DeviceHelper.getWorkDir(9));
                    this.navigationStatusSubject.onNext(new Pair<>(navByName.getNavServerId(), DeviceFileStatus.STATUS_DELETED.getStatus()));
                    return;
                }
                this.navigationStatusSubject.onNext(new Pair<>(navByName.getNavServerId(), DeviceFileStatus.STATUS_DELETE_FAIL.getStatus()));
            }
        }
    }

    @Override
    public void onFileReceived(String str) {
        if (SprintFileHelper.isNavListFile(str)) {
            loadNavFromFile(str).observeOn(AndroidSchedulers.mainThread()).subscribe((Subscriber<? super List<SprintNav>>) new Subscriber<List<SprintNav>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable th) {
                    AbstractSprintDeviceNavigationModel.this.setSprintNavList(null);
                }

                @Override
                public void onNext(List<SprintNav> list) {
                    AbstractSprintDeviceNavigationModel.this.setSprintNavList(list);
                }
            });
        }
    }

    @Override
    public void release() {
        this.controller.unregisterXossDeviceListener(this);
    }
}
